package suanfa;

import java.util.PriorityQueue;

public class MedianFinder {
    PriorityQueue<Integer> left;
    PriorityQueue<Integer> right;
    public MedianFinder() {
        left=new PriorityQueue<>((a,b)->b-a);
        right=new PriorityQueue<>((a,b)->a-b);
    }

    public void addNum(int num) {
        if(left.size()==right.size()){
            if(left.isEmpty()||num<=left.peek()){
                left.offer(num);
            }else{
                right.offer(num);
                left.offer(right.poll());
            }
        }else{
            if(num<left.peek()){
                left.offer(num);
                right.offer(left.poll());
            }else{
                right.offer(num);
            }
        }
    }

    public double findMedian() {
        if(left.size()==right.size()) return (left.peek() + right.peek())/2.0;
        else return left.peek();
    }
}
